<?php
	$s="";
	
	foreach($cal_future_result as $row)
	{
		$id=$row->id;
		$time=$row->cal_time;
		$title=$row->point_title;
		$point_x=$row->point_x;
		$point_y=$row->point_y;
		$addr=$row->point_addr;
		$type=$row->type;
		$type_cn;
		switch($type){
			case "work":
				$type_cn = "工作";break;				
			case "play":
				$type_cn = "娱乐";break;				
			case "eat":
				$type_cn = "餐饮";break;
			case "sport":
				$type_cn = "运动";break;
			case "rest":
				$type_cn = "休息";break;
			case "meet":
				$type_cn = "聚会";break;
			case "other":
				$type_cn = "其他";break;
			default:
				$type_cn = "其他";
		}
		$string=strval($type);
		
		$s=$s . '<li id=cal_' . $id . '><a class="pic '.$type.'" title="'.$title.' '.$type_cn.'" onclick=\'detail_cal("' . $id . '","' . $time . '","' . $title . '","' . $point_x . '","' . $point_y . '","' . $addr . '","' . $type . '")\'></a>'; 
		$s=$s . '<p><span>' . date('H:i',strtotime($time)) . '</span></p>';
		$s=$s . '<span class="cal-delete" onclick=\'delete_cal("'.$id.'")\'></span>';
		$s=$s . '<p><span class="cal-content" title="'.$addr.'" onclick=\'detail_cal("' . $id . '","' . $time . '","' . $title . '","' . $point_x . '","' . $point_y . '","' . $addr . '","' . $type . '")\'>' . $title . '</span></p>';
		$s=$s . '</li>';
	}
	echo $s;
?>	

<script type="text/javascript">
function detail_cal(id,time,title,point_x,point_y,addr,type){
	//清空搜索结果
	$("#results").slideUp("slow");
	$("#location_search").val("");
	//清空地图上的标记
	map.clearOverlays();
	document.getElementById("choose_cal").value=String(id);
	var point_cal = new BMap.Point(point_x,point_y);
	
	//添加气泡标记
	var marker_cal = new BMap.Marker(point_cal);
	map.addOverlay(marker_cal);

	//定义弹出提示窗口的内容

	var info=[];
	info.push('<div id="info_window">');
	info.push('你想去：'+title+'<br>');
	info.push('主题：'+type+'<br>');
	info.push('时间：'+time+'<br>');
	info.push('地址：'+addr+'<br>');
	info.push('</div>');
	
	//添加弹出提示窗口
	var infoWindow = new BMap.InfoWindow(info.join(""),{title:"您的日程",width:200,height:100});
	marker_cal.openInfoWindow(infoWindow);
	
	marker_cal.addEventListener("click", function(){this.openInfoWindow(infoWindow);});
}

//删除页面上的日程信息
var xmlhttp1;
function delete_cal(id){
	if(!window.confirm("确认删除？"))
		return;
		
	xmlhttp1=GetXmlHttpObject();
	if (xmlhttp1==null)
	{
	  	alert ("Browser does not support HTTP Request");
	  	return;
	}
	var url="calendar/delete_calender";
	xmlhttp1.onreadystatechange=function(){
		if (xmlhttp1.readyState==4 || xmlhttp1.readyState=="complete")
	 	{
			if(xmlhttp1.responseText==1)
			{
				document.getElementById("cal_"+id).innerHTML="";
				if(document.getElementById("choose_cal").value==id)
				{
					map.clearOverlays();
					
				}
			}
				
			else{
			 	alert("删除失败");
			}
			  
	 	} 
	};
	xmlhttp1.open("POST",url,true);
	xmlhttp1.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
	xmlhttp1.send("cal_id="+id);	
}



//点击在地图上显示按钮之后，显示当前对应时间的marker
function update_cal(show_marker){
	
	$("#results").hide();
	var time_type=$("#choose").val();
	xmlhttp1=GetXmlHttpObject();
	if (xmlhttp1==null)
	{
	  	alert ("Browser does not support HTTP Request");
	  	return;
	}
	var url="calendar/show_marker";
	xmlhttp1.onreadystatechange=function(){
		if (xmlhttp1.readyState==4 || xmlhttp1.readyState=="complete")
	 	{
			var xmlDoc=parseXML(xmlhttp1.responseText);
			fresh_cal(xmlDoc);
			if(show_marker)
			{
				show_markers(xmlDoc);
			}			
	 	} 
	};
	xmlhttp1.open("POST",url,true);
	xmlhttp1.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
	xmlhttp1.send("time_type="+time_type);
}

//返回xml适配方法
function parseXML(string)
{
	try //Internet Explorer
  	{
  		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  		xmlDoc.async="false";
  		xmlDoc.loadXML(string);
  	}
	catch(e)
  	{
  		try //Firefox, Mozilla, Opera, etc.
  		{
  			parser=new DOMParser();
  			xmlDoc=parser.parseFromString(string,"text/xml");
  		}
  		catch(e)
  		{
  			alert(e.message);
  			return;
  		}
	}
	return xmlDoc;
}

//根据返回的xml文件刷新list-cal中的内容
function fresh_cal(xmlDoc){
	length=xmlDoc.getElementsByTagName("id").length;
	
	var i=0;
	var html="";
	for(i=0;i<length;i++){
		var id=xmlDoc.getElementsByTagName("id")[i].childNodes[0].nodeValue;
		var time=xmlDoc.getElementsByTagName("time")[i].childNodes[0].nodeValue;
		var title=xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue;
		var addr=xmlDoc.getElementsByTagName("addr")[i].childNodes[0].nodeValue;
		var point_x=xmlDoc.getElementsByTagName("point_x")[i].childNodes[0].nodeValue;
		var point_y=xmlDoc.getElementsByTagName("point_y")[i].childNodes[0].nodeValue;
		var type=xmlDoc.getElementsByTagName("type")[i].childNodes[0].nodeValue;
		
		var type_cn;
		switch(type){
			case "work":
				type_cn = "工作";break;				
			case "play":
				type_cn = "娱乐";break;				
			case "eat":
				type_cn = "餐饮";break;
			case "sport":
				type_cn = "运动";break;
			case "rest":
				type_cn = "休息";break;
			case "meet":
				type_cn = "聚会";break;
			case "other":
				type_cn = "其他";break;
			default:
				type_cn = "其他";
		}
		
		//今天或者明天就不要显示日期了
		if($(".selected").text()=="今天" || $(".selected").text()=="明天")
		{
			time = time.substr(11,5);
		}
		else
			time = time.substr(5,11);
		
		html=html+'<li id=cal_'+id+'><a class="pic '+type+'" title="'+title+' '+type_cn+'" onclick=\'detail_cal("'+id+'","'+time+'","'+title+'","'+point_x+'","'+point_y+'","'+addr+'","'+type+'")\'></a>';
		html=html+'<p><span>'+time+'</span></p>';
		html=html+'<span class="cal-delete" onclick=\'delete_cal("'+id+'")\'></span>';
		html=html+'<p><span class="cal-content" title="'+addr+'" onclick=\'detail_cal("'+id+'","'+time+'","'+title+'","'+point_x+'","'+point_y+'","'+addr+'","'+type+'")\'>'+title+'</span></p>';
		html=html+'</li>';
	}
	$("#list-cal").html(html);
	
}

//根据返回的xml文件在地图上添加需要显示的marker
function show_markers(xmlDoc){
	length=xmlDoc.getElementsByTagName("id").length;

	map.clearOverlays();
	//保存所有的marker
	var markers=[];
	//保存所有的point
	var points=[];
	//保存所有的infoWindows
	var infoWindows=[];
	
	var i=0;
	for(i=0;i<length;i++){
		var id=xmlDoc.getElementsByTagName("id")[i].childNodes[0].nodeValue;
		var time=xmlDoc.getElementsByTagName("time")[i].childNodes[0].nodeValue;
		var title=xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue;
		var addr=xmlDoc.getElementsByTagName("addr")[i].childNodes[0].nodeValue;
		var point_x=xmlDoc.getElementsByTagName("point_x")[i].childNodes[0].nodeValue;
		var point_y=xmlDoc.getElementsByTagName("point_y")[i].childNodes[0].nodeValue;
		var type=xmlDoc.getElementsByTagName("type")[i].childNodes[0].nodeValue;

		var point_cal = new BMap.Point(point_x,point_y);
		points.push(point_cal);
		//添加气泡标记
		var marker_cal = new BMap.Marker(point_cal);
		markers.push(marker_cal);
		map.addOverlay(marker_cal);

		var info=[];
		info.push('<div id="info_window">');
		info.push('你想去：'+title+'<br>');
		info.push('主题：'+type+'<br>');
		info.push('时间：'+time+'<br>');
		info.push('地址：'+addr+'<br>');
		info.push('</div>');
		
		var infoWindow = new BMap.InfoWindow(info.join(""),{title:"您的日程",width:200,height:100});
	    infoWindows.push(infoWindow);
	    //添加窗口
	    addInfo_bytime(marker_cal,infoWindow);
		//marker_cal.addEventListener("click", openInfoWinFun);
		
		
	}
	map.setViewport(points);
	
}

function addInfo_bytime(marker,infoWindow){
	var openInfoWinFun = function(){
		marker.openInfoWindow(infoWindow);
	}
	marker.addEventListener("click", openInfoWinFun);
}

function set_choose(time){
	$("#choose").val(time);
	$(".selected").removeClass("selected");
	$("span[for='"+time+"']").addClass("selected");
	update_cal(false);
}
 
</script>  	  
 